package com.ywqf_api.service.impl;

import com.ywqf_api.base.BaseException;
import com.ywqf_api.dao.HouseKeepDao;
import com.ywqf_api.dto.in.HouseKeepDto;
import com.ywqf_api.dto.in.WaterFeeDto;
import com.ywqf_api.entity.Commodity;
import com.ywqf_api.service.HouseKeepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/4/8 0008.
 */
@Service
public class HouseKeepServiceImpl implements HouseKeepService {
    @Autowired
    private HouseKeepDao houseKeepDao;

    @Override
    public List<Commodity> findHouseKeep(Integer communityId,Integer type) {
        List<Commodity> list = houseKeepDao.findHouseKeep(communityId,type);
        if(list.size() > 0){
            for (int i=0 ; i<list.size();i++){
                Commodity commodity = list.get(i);
                Integer count = houseKeepDao.findCount(commodity.getGoodsId());
                String num = String.valueOf(count);
                commodity.setCount(num);
            }
            return list;
        }else{
            return null;
        }
    }

    @Override
    public Map findHouseKeepData(Integer goodsId) {
        Commodity commodity = houseKeepDao.findHouseKeepData(goodsId);
        if(commodity == null){
            return null;
        }
        List<Commodity> showList = houseKeepDao.findShowUrl(goodsId);
        List<Commodity> detailList = houseKeepDao.findDetUrl(goodsId);
        Map map = new HashMap();
        map.put("data",commodity);
        map.put("showPic",showList);
        map.put("detailPic",detailList);
        return map;
    }

    @Override
    @Transactional
    public int saveHouseKeep(HouseKeepDto houseKeepDto) {
        Random random = new Random();
        int fourRandom = random.nextInt(90000) + 10000;
       /* int randLength = fourRandom.length();
        if(randLength<5){
            for(int i=1; i<=5-randLength; i++)
                fourRandom = "0" + fourRandom  ;
        }*/
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
        String orderNum;
        orderNum = "CL"+longstr2+fourRandom;
        houseKeepDto.setOrderNum(orderNum);
        int save = houseKeepDao.saveOrder(houseKeepDto);
        if(save > 0){
            Integer orderId = houseKeepDto.getOrderId();
            Integer goodsId = houseKeepDto.getGoodsId();
            Double money = houseKeepDto.getOrderMoney();
            int saveGoods = houseKeepDao.saveOrderGoods(orderId,goodsId,money);
            if(saveGoods > 0){
                return orderId;
            }else{
                throw new BaseException("添加商品订单失败");
            }
        }else{
            throw new BaseException("添加订单失败");
        }
    }

    @Override
    public int saveHouseKeepCard(HouseKeepDto houseKeepDto) {
        Integer goodsId = houseKeepDto.getGoodsId();
        Integer orderId = houseKeepDto.getOrderId();
        Commodity commodity = houseKeepDao.findParamValue(goodsId);
        Integer value;
        if(commodity == null){
            value = 0;
        }else{
            value = commodity.getParamValue();
        }
        Integer customerId = houseKeepDto.getCustomerId();
        String orderNum = houseKeepDao.findOrderNum(orderId);
        int saveCard = houseKeepDao.saveCard(orderId,orderNum,value,customerId);
        if(saveCard > 0){
            return orderId;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new BaseException("添加次卡失败");
        }
    }
}
